﻿@page "/"

<h1>1. Icons</h1>
<div>
    <FluentIcon Icon="Icons.Regular.Size24.Save" />
    <FluentIcon Icon="Icons.Regular.Size24.Album" />

    <FluentIcon Value="@(Icon.FromImageUrl("/Blazor.png"))" />
    
    @(new Icons.Regular.Size20.Add().ToMarkup())
    @(new Icons.Regular.Size20.Airplane().ToMarkup("60px", "red"))
</div>

<div>
    <b>All Icons</b>
    <div>
        <FluentTextField Placeholder="search icons" @bind-Value="@IconSearch" @onkeydown="@IconsSearch_Start" />
    </div>

    @if (IconSearchInProgress)
    {
        <FluentProgressRing />
    }

    @foreach (var item in IconsFound)
    {
        <FluentIcon Value="Icons.GetInstance(item)" Title="@($"{item.Name}")" Width="32px" />
    }
</div>

<h1>2. Emojis</h1>
<div>
    <FluentEmoji Emoji="@(Emojis.PeopleBody.Color.Default.Artist)" />
    <FluentEmoji Emoji="@(Emojis.SmileysEmotion.Color.Default.RollingOnTheFloorLaughing)" Width="50px" />

    @(new Emojis.Objects.Color.Default.Accordion().ToMarkup("80px"))
    @(new Emojis.TravelPlaces.Color.Default.Ambulance().ToMarkup())
</div>

<div>
    <b>All Emojis</b>
    <div>
        <FluentTextField Placeholder="search emoji's" @bind-Value="@EmojiSearch" @onkeydown="@EmojiSearch_Start" />
    </div>

    @if (EmojiSearchInProgress)
    {
        <FluentProgressRing />
    }
    else
    {
        @foreach (var item in emojisFound)
        {
            <FluentEmoji Value="Emojis.GetInstance(item)" Title="@($"{item.Name}")" Width="64px" />
        }
    }
</div>

<sub>The first search may be slow, due to the dynamic loading of icons and emojis.</sub>

@code
{
    private bool IconSearchInProgress = false;
    private bool EmojiSearchInProgress = false;
    private string IconSearch = string.Empty;
    private string EmojiSearch = string.Empty;
    private IEnumerable<IconInfo> IconsFound = Array.Empty<IconInfo>();
    private IEnumerable<EmojiInfo> emojisFound = Array.Empty<EmojiInfo>();

    private async Task IconsSearch_Start(KeyboardEventArgs e)
    {
        if (e.Code == "Enter" || e.Code == "NumpadEnter")
        {
            IconSearchInProgress = true;
            await Task.Delay(1);

            IconsFound = Icons.AllIcons
                             .Where(i => i.Name.Contains(IconSearch, StringComparison.InvariantCultureIgnoreCase)
                                     && i.Variant == IconVariant.Regular
                                     && i.Size == IconSize.Size24)
                             .Take(100)
                             .ToArray();

            IconSearchInProgress = false;
            await Task.Delay(1);
        }
    }

    private async Task EmojiSearch_Start(KeyboardEventArgs e)
    {
        if (e.Code == "Enter" || e.Code == "NumpadEnter")
        {
            EmojiSearchInProgress = true;
            await Task.Delay(1);

            emojisFound = Emojis.AllEmojis
                                .Where(i => (i.Name.Contains(EmojiSearch, StringComparison.InvariantCultureIgnoreCase) && i.KeyWords.Contains(EmojiSearch, StringComparison.InvariantCultureIgnoreCase))
                                         && i.Style == EmojiStyle.Color
                                         && i.Skintone == EmojiSkintone.Default)
                                .Take(100)
                                .ToArray();

            EmojiSearchInProgress = false;
            await Task.Delay(1);
        }

    }
}